
package com.clear.honghefinance.biz.modules.assist_calculate_summary.handler.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.clear.honghefinance.biz.modules.assist_calculate_summary.domain.dto.*;
import com.clear.honghefinance.biz.modules.system.member.domain.vo.AdminDTO;
import com.clear.honghefinance.biz.modules.assist_calculate.enums.AssistCalculateCategoryCodeEnum;
import com.clear.honghefinance.biz.modules.assist_calculate_summary.converter.AssistCalculateProjectConvertor;
import com.clear.honghefinance.biz.modules.assist_calculate_summary.domain.entity.AssistCalculateProject;
import com.clear.honghefinance.biz.modules.assist_calculate_summary.domain.vo.GetAssistCalculateProjectVO;
import com.clear.honghefinance.biz.modules.assist_calculate_summary.domain.vo.ListAssistCalculateBaseVO;
import com.clear.honghefinance.biz.modules.assist_calculate_summary.handler.AssistCalculateHandler;
import com.clear.honghefinance.biz.modules.assist_calculate_summary.service.IAssistCalculateProjectService;
import com.clear.honghefinance.common.exception.BizException;
import com.clear.honghefinance.common.service.TokenService;
import com.clear.honghefinance.common.util.DateUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;

/**
 * 项目辅助核算Handler
 */
@Component
@Slf4j
public class AssistCalculateProjectHandler implements AssistCalculateHandler {

    @Resource
    private IAssistCalculateProjectService assistCalculateProjectService;
    @Resource
    private TokenService<AdminDTO> tokenService;

    /**
     * 枚举身份的识别
     *
     * @return
     */
    @Override
    public AssistCalculateCategoryCodeEnum getHandlerType() {
        return AssistCalculateCategoryCodeEnum.PROJECT;
    }

    /**
     * 创建项目辅助核算
     *
     * @param dto
     */
    @Override
    public boolean create(Object dto) {
        AssistCalculateProject assistCalculate = AssistCalculateProjectConvertor.INSTANCE.convertDTOToEntity((CreateAssistCalculateProjectDTO) dto);
        assistCalculate.initDefault();
        assistCalculate.setTenantId(tokenService.getThreadLocalTenantId());
        assistCalculate.setMemberId(tokenService.getThreadLocalUserId());
        assistCalculate.setUpdateMemberId(tokenService.getThreadLocalUserId());

        if (assistCalculateProjectService.save(assistCalculate)) {
            return true;
        }
        throw new BizException("创建项目辅助核算失败");
    }

    /**
     * 修改项目辅助核算
     *
     * @param dto
     * @return
     */
    @Override
    public boolean update(Object dto) {
        AssistCalculateProject assistCalculate = AssistCalculateProjectConvertor.INSTANCE.convertDTOToEntity((UpdateAssistCalculateProjectDTO) dto);
        assistCalculate.setUpdateMemberId(tokenService.getThreadLocalUserId());
        assistCalculate.setUpdateTime(DateUtil.getSystemTime());
        LambdaUpdateWrapper<AssistCalculateProject> lambdaUpdateWrapper = Wrappers.<AssistCalculateProject>lambdaUpdate()
                .eq(AssistCalculateProject::getAssistCalculateSummaryId, assistCalculate.getId())
                .eq(AssistCalculateProject::getTenantId, assistCalculate.getTenantId())
                .eq(AssistCalculateProject::getDelFlag, false);

        if (assistCalculateProjectService.update(assistCalculate, lambdaUpdateWrapper)) {
            return true;
        }
        throw new BizException("修改项目辅助核算失败");
    }

    /**
     * 根据辅助核算id查询项目辅助核算列表
     *
     * @param ids 辅助核算id列表
     * @return
     */
    @Override
    public List<? extends ListAssistCalculateBaseVO> listByAssistCalculateSummaryIds(List<Long> ids) {
        try {
            LambdaQueryWrapper<AssistCalculateProject> lambdaQueryWrapper = Wrappers.<AssistCalculateProject>lambdaQuery()
                    .eq(AssistCalculateProject::getTenantId, tokenService.getThreadLocalTenantId())
                    .eq(AssistCalculateProject::getDelFlag, false)
                    .in(CollectionUtils.isNotEmpty(ids), AssistCalculateProject::getAssistCalculateSummaryId, ids)
                    .select(AssistCalculateProject::getId,
                            AssistCalculateProject::getDisable,
                            AssistCalculateProject::getAssistCalculateSummaryId,
                            AssistCalculateProject::getResponsibleDepartment,
                            AssistCalculateProject::getResponsiblePerson,
                            AssistCalculateProject::getPhone,
                            AssistCalculateProject::getStartDate,
                            AssistCalculateProject::getEndDate
                    );
            List<AssistCalculateProject> list = assistCalculateProjectService.list(lambdaQueryWrapper);

            return AssistCalculateProjectConvertor.INSTANCE.convertEntityToVO(list);
        } catch (Exception e) {
            throw new BizException("查询项目辅助核算列表失败");
        }
    }

    /**
     * 根据辅助核算id查询辅助核算详情
     *
     * @param id 辅助核算id
     * @return
     */
    @Override
    public GetAssistCalculateProjectVO getDetailById(long id) {
        try {
            LambdaQueryWrapper<AssistCalculateProject> lambdaQueryWrapper = Wrappers.<AssistCalculateProject>lambdaQuery()
                    .eq(AssistCalculateProject::getTenantId, tokenService.getThreadLocalTenantId())
                    .eq(AssistCalculateProject::getDelFlag, false)
                    .eq(AssistCalculateProject::getAssistCalculateSummaryId, id)
                    .select(AssistCalculateProject::getId,
                            AssistCalculateProject::getDisable,
                            AssistCalculateProject::getAssistCalculateSummaryId,
                            AssistCalculateProject::getResponsibleDepartment,
                            AssistCalculateProject::getResponsiblePerson,
                            AssistCalculateProject::getPhone,
                            AssistCalculateProject::getStartDate,
                            AssistCalculateProject::getEndDate
                    );

            AssistCalculateProject assistCalculate = assistCalculateProjectService.getOne(lambdaQueryWrapper);

            return AssistCalculateProjectConvertor.INSTANCE.convertEntityToVO(assistCalculate);
        } catch (Exception e) {
            throw new BizException("查询项目辅助核算详情失败");
        }
    }

}
